package com.common;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

/**
 * Created by ztc on 16-1-11.下午9:26
 */
public class SendMail extends Thread{
    String addr=null;
    String subject=null;
    String text=null;
    String pic=null;
    public SendMail(String addr,String subject,String text,String pic){
        this.addr=addr;
        this.subject=subject;
        this.text=text;
        this.pic=pic;
    }
    public void run(){
        Properties properties=new Properties();
        properties.setProperty("mail.host",Constant.mailHost);
        properties.setProperty("mail.transport.protocol","smtp");
        properties.setProperty("mail.smtp.auth","true");
        //1.获取ｓｅｓｓｉｏｎ
        Session session=Session.getInstance(properties);
        //session.setDebug(true);
        try {
            //2.通过session获得transport
            Transport transport=session.getTransport();
            //3.transport登陆邮箱
            transport.connect(Constant.mailHost,Constant.mailUser,Constant.mailPw);
            //4.创建message
            Message message=createMessage(session);
            //5.发送创建的邮件
            transport.sendMessage(message,message.getAllRecipients());
            transport.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    private Message createMessage(Session session){
        Message message=new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(Constant.mailUserAddr));
            //遍历出收件人邮箱地址
            String[] rcpts=addr.split(";");
            InternetAddress[] recipients=new InternetAddress[rcpts.length];
            for(int i=0;i<rcpts.length;i++){
                System.out.println(rcpts[i]);
                recipients[i]=new InternetAddress(rcpts[i]);
            }
            //设置收件人
            message.setRecipients(Message.RecipientType.TO,recipients);
            //设置主题
            message.setSubject(subject);
            //判断是否是有图片的邮件
            if(!pic.contains(".")) {
                message.setContent(text, "text/html;charset=UTF-8");
            }else{
                MimeBodyPart txt=new MimeBodyPart();
                String[] cid=pic.split("\\.");
                txt.setContent(text+"<img src='cid:fixpic."+cid[1]+"'>","text/html;charset=UTF-8");
                MimeBodyPart image=new MimeBodyPart();
                DataHandler dataHandler=new DataHandler(new FileDataSource(pic));
                image.setDataHandler(dataHandler);
                image.setContentID("fixpic."+cid[1]);
                MimeMultipart mm=new MimeMultipart();
                mm.addBodyPart(txt);
                mm.addBodyPart(image);
                mm.setSubType("related");
                message.setContent(mm);
                message.saveChanges();
                //message.writeTo(new FileOutputStream("/home/ztc/software/test.email"));
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return message;
    }

}
